﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class VirtualCustomize : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Role"].ToString().Equals("ShopOwner"))
            {
                Panel1.Visible = true;
                Panel2.Visible = false;

                if (!IsPostBack)
                {
                    SqlCommand cmdSearch;
                    SqlDataReader dtrSearch;
                    conn.Open();

                    cmdSearch = new SqlCommand("SELECT * FROM Virtual_Shop WHERE u_id=@id", conn);
                    cmdSearch.Parameters.AddWithValue("@id", 1);
                    dtrSearch = cmdSearch.ExecuteReader();

                    dtrSearch.Read();

                    if (Convert.ToInt32(dtrSearch["layout"]) == 1)
                        RadioButton1.Checked = true;
                    else if (Convert.ToInt32(dtrSearch["layout"]) == 2)
                        RadioButton2.Checked = true;

                    ddlColor.SelectedValue = dtrSearch["bgColor"].ToString();
                    ddlFont.SelectedValue = dtrSearch["font"].ToString();

                    dtrSearch.Close();
                    conn.Close();
                }
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
            }            
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlCommand cmdIn;
            conn.Open();
            int vs = 0;

            cmdIn = new SqlCommand("UPDATE Virtual_Shop SET bgColor=@color, font=@font, layout=@layout WHERE u_id=@id", conn);
            cmdIn.Parameters.AddWithValue("@color", ddlColor.SelectedValue);
            cmdIn.Parameters.AddWithValue("@font", ddlFont.SelectedValue);

            if (RadioButton1.Checked == true)
                vs = 1;
            else if (RadioButton2.Checked == true)
                vs = 2;

            cmdIn.Parameters.AddWithValue("@layout", vs);
            cmdIn.Parameters.AddWithValue("@id", 1);
            cmdIn.ExecuteNonQuery();

            conn.Close();
            
            MessageBox("Updated!");
        }

        protected void btnPreview_Click(object sender, EventArgs e)
        {
            Label lbl = new Label();

            int vs = 0;
            if (RadioButton1.Checked == true)
                vs = 1;
            else if (RadioButton2.Checked == true)
                vs = 2;

            String color = ddlColor.SelectedValue;

            String ccolor = color.Replace("#", "");

            String url = "id=1&l=" + vs + "&c=" + ccolor + "&f=" + ddlFont.SelectedValue;
            lbl.Text = "<script language='javascript'>window.open('VirtualPreview.aspx?"+url+"', 'Preview', 'menubar=no,channelmode=yes,width=900,height=600');</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnRequestOpenShop_Click(object sender, EventArgs e)
        {
            Response.Redirect("VirtualAgreement.aspx");
        }

    }
}